﻿using HolaMundoMVC4.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HolaMundoMVC4.Controllers
{
    public class HomeController : Controller
    {
        private LinkContenedor linkContenedor = new LinkContenedor();

        //
        // GET: /Home/

        public ActionResult Index()
        {
            var laListaDeLinks = linkContenedor.links;
            // laListaDeLinks.Count();
            return View(model: laListaDeLinks);
        }

        [HttpGet]
        public ActionResult Agregar()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Agregar(Link link)
        {
            linkContenedor.agregarLink(link); 
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Editar(int id)
        {
            Link link = linkContenedor.links.Find(id);
            return View(model: link);
        }

        [HttpPost]
        public ActionResult Editar(Link link)
        {
            Link linkBase = linkContenedor.links.Find(link.id);
            linkBase.url = link.url;
            linkBase.descripcion = link.descripcion;

            linkContenedor.SaveChanges();
            
            return RedirectToAction("Index");
        }

    }
}
